<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.tooltip');
?>
<style>
.is_required_mark {
	color:red;
}
</style>

<script language="javascript" type="text/javascript">

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

// Array of field must be check before submit data
var arrCheck = new Array();

function submitbutton(pressbutton) {
	var form = document.adminForm;

	if (pressbutton == 'cancel') {
		submitform( pressbutton );
		return;
	}
	if ( pressbutton == 'savefield' || pressbutton == 'apply' ){
		
		var account_create = getSelectedRadioValue(form.account_create);
		
		switch (account_create){
			case "ONLY_DEFAULT" :{
				if(form.default_user.value == '' ){
				    alert('<?php echo JText::_('PLEASE SELECT DEFAULT USER')?>');
					form.default_user.focus();
					return;
				}
				break;
			}
			default :{
				if(form.default_user.value == '' && account_create == 'IMPORT_DEFAULT'){
				    alert('<?php echo JText::_('PLEASE SELECT DEFAULT USER')?>');
					form.default_user.focus();
					return;
				}
								
				if(form.ac_Username.value == '' ){
				    alert('<?php echo JText::_('PLEASE SELECT ACCOUNT USER NAME')?>');
					form.ac_Username.focus();
					return;
				}
				if(form.ac_Password.value == '' && !form.ac_Password.disabled){
				    alert('<?php echo JText::_('ACCOUNT PASSWORD CANNOT BE BLANK')?>');
					form.ac_Password.focus();
					return;
				}
				if(form.ac_Email.value == '' ){
				    alert('<?php echo JText::_('PLEASE SELECT ACCOUNT E-MAIL')?>');
					form.ac_Email.focus();
					return;
				}
				if(form.ac_FirstName.value == '' ){
				    alert('<?php echo JText::_('PLEASE SELECT ACCOUNT FULLNAME')?>');
					form.ac_FirstName.focus();
					return;
				}
				break;
			}
		}
		
		// Check job title
		if(form.job_identifier.value == ''){
			alert('<?php echo JText::_('PLEASE SELECT XML FIELD FOR JOB IDENTIFIER')?>');
			form.job_identifier.focus();
			return;
		}
		// Dynamic check data
		for(i=0; i < arrCheck.length; i++){
			if(typeof(arrCheck[i]) != 'undefined'){
				arrProccess = arrCheck[i].split(",");
				switch(arrProccess[0]){
					case "text": {
						if(document.getElementById("ignore"+i).checked){
							if(document.getElementById("replace"+i).value == ""){
								alert(arrProccess[1] + " <?php echo JText::_('MUST HAVE VALUE')?>.");
								return;
							}
						}else{
							if(document.getElementById("xml_element_"+i).value == ""){
								alert(arrProccess[1] + " <?php echo JText::_('MUST HAVE VALUE')?>.");
								return;
							}
						}
						break;
					}
					case "select": {
						
						if(getSelectedRadioValue(document.getElementsByName("cat_mode"+i)) != "ADD_DEFAULT" && getSelectedRadioValue(document.getElementsByName("cat_mode"+i)) != "ADD_NEW" ){
							alert(arrProccess[1] + " <?php echo JText::_('MUST HAVE VALUE')?>.");
							return;
						}
						break;
					}
				}
			}
		}
		
		submitform( pressbutton );
	}					
	else {
		submitform( pressbutton );
	}
}
	/**
	 * Change disable properties
	 * @params 
	 */
	function changeDisableProperties(checked, passDisable){
		// AdminForm object
		var frm = document.adminForm;
		
		var checkedStatus = "";
		if(checked)
			checkedStatus = "disabled";
		
		frm.ac_Username.disabled = checkedStatus;
		frm.ac_Password.disabled = passDisable;
		frm.ac_Email.disabled = checkedStatus;
		frm.ac_FirstName.disabled = checkedStatus;
	}
</script>

<form action="index.php" method="post" name="adminForm" enctype="multipart/form-data" id="adminForm">
<div class="col100">
	<?php 
	echo $this->menu_User();
	// Set, get the variable
	
	$feed_id = (int) $this->data->FeedID;
	$feed = JAJobBoardModelJAXMLImport::fcnLoadFeedRow($feed_id);
	
	if (JRequest::getVar("error", "false") == "false") {
		$feed['FMD']->fcnFillRequest();
	} else {
		$feed['FMD']->fcnFillOptions();
	}
	
	if(JRequest::getVar("account_create") == "")
		JRequest::setVar("account_create", "ONLY_DEFAULT");
	
	?>
        	<br/>
	<fieldset class="adminform">	
		<legend><?php echo JText::_( 'ACCOUNT OPTIONS' ); ?></legend>
		<table class="admintable" width="100%" border="1px" style="border-collapse:collapse;  border-color:#CCCCCC" cellspacing="2" cellpadding="4">
		<tr bgcolor="white">
			<td colspan="1" align="right" class="key">
				<span class="editlinktip hasTip" title="<?php echo JText::_("EMPLOYER'S ACCOUNTS"); ?>::<?php echo JText::_("HOW TO ASSOCIATE THE JOBS WITH EMPLOYER'S ACCOUNTS?"); ?>">
					<?php echo JText::_("EMPLOYER'S ACCOUNTS"); ?>: 
				</span>	
			</td>			
			<td colspan="2">
			<input type="radio" onchange="changeDisableProperties(true, true);" name="account_create" value="ONLY_DEFAULT"  <?php echo (JRequest::getVar("account_create", "ONLY_DEFAULT") == 'ONLY_DEFAULT')? 'checked="checked"':''; ?>> <?php echo JText::_("ALWAYS IMPORT THE JOBS UNDER THE <B>DEFAULT USERNAME</B>");?>
			<span class="is_required_mark">*</span>: <?php echo $this->getSystemUsers(JRequest::getVar("default_user")); ?><br/>
			<input type="radio" onchange="changeDisableProperties(false, true);" name="account_create" value="IMPORT_DEFAULT" <?php echo (JRequest::getVar("account_create") == 'IMPORT_DEFAULT')? 'checked':''; ?>> <?php echo JText::_("INSERT USING THE EMPLOYER'S ACCOUNT, BUT INSERT USING THE <B>DEFAULT USERNAME</B> IF USER/PASS DO NOT AUTHENTICATE<BR>");?>
			<input type="radio" onchange="changeDisableProperties(false, true);" name="account_create" value="IMPORT_REJECT" <?php echo ( JRequest::getVar("account_create") == 'IMPORT_REJECT')? 'checked':''; ?> > <?php echo JText::_("INSERT USING THE EMPLOYER'S ACCOUNT DETAILS PROVIDED WITH THE FEED. REJECT IF A USER/PASS DOES NOT AUTHENTICATE<BR>");?>
			<input type="radio" onchange="changeDisableProperties(false, false);" name="account_create" value="IMPORT_CREATE" <?php echo (JRequest::getVar("account_create") == 'IMPORT_CREATE')? 'checked':''; ?>> <?php echo JText::_("INSERT USING THE EMPLOYER'S USERNAME, CREATE A NEW ACCOUNT FROM THE ACCOUNT DATA PRESENT IN THE FEED.<BR>");?>
		</tr>
		<tr bgcolor="white">
			<td colspan="1" align="right" class="key">
				<span class="editlinktip hasTip" title="<?php echo JText::_("ACCOUNT FIELDS");?>::<?php echo JText::_("ACCOUNT FIELDS");?>">
					<?php echo JText::_("ACCOUNT FIELDS");?>: 
				</span>	
			</td>
			
			<td colspan="2"><!--<span class="is_required_mark">*</span><?php echo JText::_("IF YOU SELECTED ANY OF THE FIRST THREE OPTIONS ABOVE, THEN YOU WILL NEED TO SETUP YOUR ACCOUNT DATA FIELDS: <BR/> (THE FOLLOWING ARE REQUIRED: USERNAME, PASSWORD, EMAIL, FULL NAME)<BR>");?>-->
			<table border="0" cellSpacing="1" cellPadding="3" bgColor="#d9d9d9" >
				<tr bgcolor="white">
					<td><?php echo JText::_("USERNAME");?>:<span class="is_required_mark">*</span></td>
					<td><select <?php if(JRequest::getVar("account_create", "ONLY_DEFAULT") == "ONLY_DEFAULT") echo 'disabled="disabled"'; ?> <?php if ( JRequest::getVar('ac_Username') != '') { ?> style="font-weight: bold" <?php } ?> type="select" name="ac_Username">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('ac_Username')); ?>
						</select>
					</td>
				</tr>
				 <tr bgcolor="white">
					<td><?php echo JText::_("PASSWORD");?>:<span class="is_required_mark">*</span></td>
					<td>
						<input <?php if(JRequest::getVar("account_create", "ONLY_DEFAULT") != "IMPORT_CREATE") echo 'disabled="disabled"'; ?> type="password" value="<?php echo JRequest::getVar('ac_Password');?>" name="ac_Password" />
					</td>
				</tr> 
				<tr bgcolor="white">
					<td><?php echo JText::_("ACCOUNT EMAIL");?>:<span class="is_required_mark">*</span></td>
					<td><select <?php if(JRequest::getVar("account_create", "ONLY_DEFAULT") == "ONLY_DEFAULT") echo 'disabled="disabled"'; ?> <?php if (JRequest::getVar('ac_Email')!='') { ?> style="font-weight: bold" <?php } ?> type="select" name="ac_Email">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('ac_Email')); ?>
						</select>
					</td>
				</tr>
				<tr bgcolor="white">
					<td><?php echo JText::_("FULL NAME");?>:<span class="is_required_mark">*</span></td>
					<td><select <?php if(JRequest::getVar("account_create", "ONLY_DEFAULT") == "ONLY_DEFAULT") echo 'disabled="disabled"'; ?> type="select" <?php if (JRequest::getVar('ac_FirstName')!='') { ?> style="font-weight: bold" <?php } ?> name="ac_FirstName">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('ac_FirstName')); ?>
						</select>
					</td>
				</tr>
					
			</table>
			
			</td>
		</tr>
		</table>
		</fieldset>
		
		<fieldset class="adminform">	
		<legend><?php echo JText::_( 'MAP YOUR FIELDS TO THE XML ATTRIBUTES' ); ?></legend>
		<table class="admintable" width="100%" border="1px" style="border-collapse:collapse;  border-color:#CCCCCC" cellspacing="2" cellpadding="4">
		<tr bgColor="#eaeaea">
			<td><b><?php echo JText::_("NAME");?></b></td>
			<td><b><?php echo JText::_("XML FIELD");?></b></td>
			<td><b><?php echo JText::_("OPTIONS");?></b></td>
		</tr>
		
		<tr bgcolor="white">
				<td align="center" style="background-color:#F6F6F6">
					<strong><?php echo JText::_("JOB IDENTIFIER");?><span class="is_required_mark">*</span></strong><br> (<?php echo JText::_("DATA TYPE: TEXT");?>)
				</td>
				<td width="10%">
					<select style="font-size: 10pt" type="select" name="job_identifier">
					<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
					<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('job_identifier')); ?>
					</select>
					</td>
					<td>
					<?php echo JText::_("THE SPECIFIC IDENTIFIER FOR EACH JOB");?>.
					</td>				
			</tr>
		<?php 
			// Get system field
			$editList = getEditFieldList("ja_jobs"," AND f.display<>'hide_all' ");
			
			foreach ($editList as $k=>$v){
				// Show map field for text field
								
				$field_type = trim( strtoupper($v->field_type));
				if ($v->name == "apply_type")
					continue;
								
				if($field_type != 'DATE_CAL' && $field_type != 'DATE' && $field_type != 'SELECT' && $field_type != 'MSELECT'){
					?>
					<tr bgcolor="white">
					<td align="center" style="background-color:#F6F6F6">
						<strong><?php echo $v->label; ?><span class="is_required_mark"><?php if($v->is_required == "1") echo '<script language="javascript" type="text/javascript"> arrCheck['.$v->id.'] = "text,'.$v->label.'"; </script>*'; ?></span></strong><br> (<?php echo JText::_("DATA TYPE: TEXT");?>)
					</td>
					<td width="10%">
						<select style="font-size: 10pt" type="select" name="xml_element_<?php echo $v->id; ?>" id="xml_element_<?php echo $v->id; ?>">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('xml_element_'.$v->id)); ?>
						</select>
						</td>
						<td>
						<?php echo JText::_("VALIDATE");?> <?php echo $this->getValidateOptions("validate".$v->id, JRequest::getVar('validate'.$v->id));?><br>
							<input id="ignore<?php echo $v->id;?>" name="ignore<?php echo $v->id;?>" value="Y" <?php if ( JRequest::getVar('ignore'.$v->id) == 'Y' ) { echo ' checked '; } ?> style="font-size: 8pt;" type="checkbox"> <?php echo JText::_("IGNORE FIELD & REPLACE WITH"); ?>: <input style="font-size: 8pt;" name="replace<?php echo $v->id;?>" id="replace<?php echo $v->id;?>" value="<?php echo JRequest::getVar('replace'.$v->id);?>" size="20" type="text"><br>
							<input name="allow_html<?php echo $v->id;?>" value="Y" type="checkbox" <?php if (JRequest::getVar('allow_html'.$v->id)=='Y') echo 'checked'; ?> > <?php echo JText::_("ALLOW LIMITED HTML");?>
						</td>				
					</tr>
					<?php
				}elseif($field_type == 'SELECT' || $field_type == 'MSELECT') {					
					?>
					<tr bgcolor="white">
					<td align="center" style="background-color:#F6F6F6">
						<strong><?php echo $v->label;?><span class="is_required_mark"><?php if($v->is_required == "1") echo '<script language="javascript" type="text/javascript"> arrCheck['.$v->id.'] = "select,'.$v->label.'"; </script>*'; ?></span></strong><br> (<?php echo JText::_("DATA TYPE: NUMBER - CATEGORY IDENTIFIER");?>)
					</td>
					<td width="10%">
						<select style="font-size: 10pt" type="select" name="xml_element_<?php echo $v->id; ?>" id="xml_element_<?php echo $v->id; ?>">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('xml_element_'.$v->id)); ?>
						</select>
						</td>
						<td>
						<?php echo JText::_("VALIDATE");?> <?php echo $this->getValidateOptions("validate".$v->id, JRequest::getVar('validate'.$v->id));?>
						<br>
						<?php 
						
						if (JRequest::getVar('cat_mode'.$v->id) == false)
								JRequest::setVar('cat_mode'.$v->id, 'ADD_DEFAULT');
						?>
							<span style="color: black; font-size: 10pt;"><?php echo JText::_("IF THE IMPORTED VALUE DOES <B>NOT EXIST</B> AS A <B>CATEGORY</B> ON YOUR DATABASE, THEN"); ?>:</span><br>
							<input name="cat_mode<?php echo $v->id; ?>" value="ADD_DEFAULT" <?php if (JRequest::getVar('cat_mode'.$v->id) == 'ADD_DEFAULT') echo 'checked'; ?> type="radio"> - <?php echo JText::_("USE DEFAULT VALUE") ?>: 
							<?php echo $this->getComboboxData($v, "parent_category_default".$v->id, "#__".$v->referred_table, $v->referred_key, $v->referred_value, JRequest::getVar('parent_category_default'.$v->id), true, "");?>
							<br>
							<input name="cat_mode<?php echo $v->id; ?>" value="ADD_NEW" <?php if (JRequest::getVar('cat_mode'.$v->id) == 'ADD_NEW') echo 'checked'; ?> type="radio"> - <?php if($v->referred_table != '') echo JText::_("ADD THE VALUE AS A NEW CATEGORY, UNDER THE CATEGORY OF"); else echo JText::_("ADD THE VALUE AS A NEW CATEGORY"); ?>  
							<?php if($v->referred_table != '') echo $this->getComboboxData($v, "parent_category".$v->id, "#__".$v->referred_table, $v->referred_key, $v->referred_value, JRequest::getVar('parent_category'.$v->id), true);?>						
							<br>							
							<input name="cat_mode<?php echo $v->id; ?>" <?php if (JRequest::getVar('cat_mode'.$v->id) == 'ADD_MATCH') echo 'checked'; ?> value="ADD_MATCH" type="radio"> - <?php echo JText::_("ATTEMPT TO MATCH THE CATEGORY NAME WITH TEXT FROM") ?> 
							<select type="select" name="cat_match<?php echo $v->id; ?>">
							<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
							<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('cat_match'.$v->id)); ?></select><br>
							<!-- <input name="cat_mode<?php echo $v->id; ?>" <?php if (JRequest::getVar('cat_mode'.$v->id) == 'ERROR') echo 'checked'; ?> value="ERROR" type="radio"> - <?php echo JText::_("THROW AN ERROR & SKIP THE WHOLE RECORD"); ?><br>
							<input name="cat_mode<?php echo $v->id; ?>" <?php if (JRequest::getVar('cat_mode'.$v->id) == 'IGNORE') echo 'checked'; ?> value="IGNORE" type="radio"> - <?php echo JText::_("DON'T DO ANYTHING, IMPORT ANYWAY"); ?> -->
					</td>		
					</tr>
					<?php
				}elseif($field_type == 'DATE_CAL'||$field_type == 'DATE'){
					?>
					<tr bgcolor="white">
					<td align="center" style="background-color:#F6F6F6">
						<strong><?php echo $v->label; ?><span class="is_required_mark"><?php if($v->is_required == "1") echo '<script language="javascript" type="text/javascript"> arrCheck['.$v->id.'] = "text,'.$v->label.'"; </script>*'; ?></span></strong><br> (<?php echo JText::_("DATA TYPE: TEXT");?>)
					</td>
					<td width="10%">
						<select style="font-size: 10pt" type="select" id="xml_element_<?php echo $v->id; ?>" name="xml_element_<?php echo $v->id; ?>">
						<option value="">[<?php echo JText::_("SELECT FIELD");?>]</option>
						<?php JAJobBoardModelJAXMLImport::fcnXMLElementOptionsList($feed, JRequest::getVar('xml_element_'.$v->id)); ?>
						</select>
						</td>
						<td>
						<?php echo JText::_("VALIDATE");?> <?php echo $this->getValidateOptions("validate".$v->id, JRequest::getVar('validate'.$v->id));?><br>
							<?php echo JText::_("OR");?>, <input id="ignore<?php echo $v->id;?>" name="ignore<?php echo $v->id;?>" value="Y" <?php if ( JRequest::getVar('ignore'.$v->id) == 'Y' ) { echo ' checked '; } ?> style="font-size: 8pt;" type="checkbox"> <?php echo JText::_("IGNORE FIELD & REPLACE WITH"); ?>: 
							<?php
								echo JHTML::calendar((JRequest::getVar('replace'.$v->id,'') == '')?date("Y-m-d"):JRequest::getVar('replace'.$v->id,''), "replace".$v->id, "replace".$v->id);
							?>
							<br>
							<!-- <input name="allow_html<?php echo $v->id;?>" value="Y" type="checkbox" <?php if (JRequest::getVar('allow_html'.$v->id)=='Y') echo 'checked'; ?> > - <?php echo JText::_("ALLOW LIMITED HTML");?> -->
						</td>				
					</tr>
					<?php
				}
			}
		?>
					
	   </table>
	</fieldset>
</div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_jajobboard" />
<input type="hidden" name="FeedID" value="<?php echo $this->data->FeedID;?>" />
<input type="hidden" name="task" value="savefield" />
<input type="hidden" name="tab" value="feeds" />
<input type="hidden" name="seq" value="<?php echo $feed["FMD"]->seq; ?>">
<input type="hidden" name="view" value="jaxmlimport" />
</form>